
<template>
  <div>
    <h2>不良品项</h2>

    不良产品编号
    <el-input
      v-model="BianHoa"
      placeholder="不良品项编号"
      style="width: 250px"
    ></el-input>

    不良产品名称
    <el-input
      v-model="DefName"
      placeholder="不良品项编号"
      style="width: 250px"
    ></el-input>

    <el-button type="primary" @click="Show()">查询</el-button>
    <br />
    <br />
    <el-button type="success" @click="dialogVisible = true">添加 </el-button>

    <!--编辑子路由-->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="60%">
      <Deftive @clickDefAdd="clickDefAdd"> </Deftive>
    </el-dialog>

    &nbsp;
    <!--修改页面-->
    <el-button
      type="warning"
      @click="(dialogVisibleUptps = true), UptZiLu()"
      width="60%"
      >修改
    </el-button>

    <el-dialog :visible.sync="dialogVisibleUptps" v-if="dialogVisibleUptps">
      <DeftiveUptz :defid="defid" @DefisePerUpd="DefisePerUpd"
        >修改
      </DeftiveUptz>
    </el-dialog>
    &nbsp;
    <el-button type="danger" @click="DleteS()">批量删除</el-button>
    &nbsp;
    <el-button type="warning" @click="DaoChus">导出</el-button>

    <el-button type="warning" @click="DaoRun">导入</el-button>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="Defective_Id" label="" width="180">
      </el-table-column>
      <el-table-column
        prop="Defective_BianHao"
        label="不良品项编号"
        width="180"
      >
      </el-table-column>

      <!--不良名称-->
      <el-table-column prop="Defective_Name" label="不良品项名称">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="(dialogVisibleUpt = true), FanTian(scope.row.Defective_Id)"
            >{{ scope.row.Defective_Name }}</el-button
          >

          <el-dialog
            title="提示"
            :visible.sync="dialogVisibleUpt"
            width="30%"
            :before-close="handleClose"
          >
            <table>
              <tr>
                <td>不良品项编号</td>
                <td>
                  <el-input
                    v-model="form.Defective_BianHao"
                    placeholder="请输入内容"
                  ></el-input>
                </td>
                <td>不良品项名称</td>
                <td>
                  <el-input
                    v-model="form.Defective_Name"
                    placeholder="请输入内容"
                  ></el-input>
                </td>
              </tr>

              <tr>
                <td>附件</td>
                <td>
                  <el-upload
                    class="upload-demo"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    multiple
                    :limit="3"
                    :on-exceed="handleExceed"
                    :file-list="fileList"
                    v-model="form.Defective_attachments"
                  >
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">
                      只能上传jpg/png文件，且不超过500kb
                    </div>
                  </el-upload>
                </td>
                <td>照片</td>
                <td>
                  <el-upload
                    class="avatar-uploader"
                    action="http://localhost:5255/api/IDefective/UploadFile"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                  >
                    <img
                      v-if="form.Defective_Img"
                      :src="'http://localhost:5255/PIc/' + form.Defective_Img"
                      class="avatar"
                      width="100px"
                      height="100px"
                    />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </td>
              </tr>
              <tr>
                <td>
                  <el-button type="success" @click="Upt()">修改</el-button>
                </td>
              </tr>
            </table>
            <span slot="footer" class="dialog-footer"> </span>
          </el-dialog>
        </template>
      </el-table-column>

      <el-table-column prop="Defective_attachments" label="附件" width="180">
      </el-table-column>

      <el-table-column prop="Defective_Img" label="图片" width="180">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="'http://localhost:5255/PIc/' + scope.row.Defective_Img"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column prop="UpdateDate" label="创建时间" width="180">
        <template slot-scope="scope">
          <label>{{ scope.row.UpdateDate.substring(0, 10) }}</label>
        </template>
      </el-table-column>

      <el-table-column prop="CreatorId" label="创建人" width="180">
        <template slot-scope="CreatorId">
          <label v-if="CreatorId.row.CreatorId == 1">超级管理员</label>
        </template>
      </el-table-column>

      <el-table-column prop="UpdateDate" label="修改时间" width="180">
        <template slot-scope="scope">
          <label>{{ scope.row.UpdateDate.substring(0, 10) }}</label>
        </template>
      </el-table-column>

      <el-table-column prop="CreatorId" label="修改人" width="180">
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import Deftive from "../../components/DefectiveComponent/DeftiveAdd.vue";
import DeftiveUptz from "../../components/DefectiveComponent/DeftiveUpt.vue";
export default {
  components: {
    Deftive,
    DeftiveUptz,
  },
  data() {
    return {
      defid: 0, //修改页面id

      form: {
        Defective_BianHao: "",
        Defective_Name: "",
        Defective_attachments: "jpg/png",
        Defective_Img: "",
        Defective_Id: 0,
      },
      dialogVisibleUptps: false,
      DefName: "",
      BianHoa: "",
      dialogVisibleUpt: false, //修改
      tableData: [],
      dialogVisible: false,
      tabs: "",

      fileList: [
        {
          name: "food.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
        {
          name: "food2.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
      ],
    };
  },
  methods: {
    //关闭修改子路由
    DefisePerUpd() {
      this.dialogVisibleUptps = false;
      this.Show();
    },

    //关闭添加子路由
    clickDefAdd() {
      this.dialogVisible = false;
      this.Show();
    },

    //传递子路由的id
    UptZiLu() {
      let ids = 0;
      this.tabs.forEach((dcs) => {
        ids = dcs.Defective_Id;
      });
      this.defid = ids;
    },

    //复选框的值
    handleSelectionChange(val) {
      this.tabs = val;
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    handleAvatarSuccess(res) {
      this.form.Defective_Img = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    //删除
    DleteS() {
      let sq = [];

      this.tabs.forEach((dcs) => {
        sq.push(dcs.Defective_Id);
      });

      this.axios
        .put("http://localhost:5255/api/IDefective/DeleteDefective", sq)
        .then((dcs) => {
          if (dcs.data) {
            this.$message.success("删除成功");
            this.Show();
          } else {
            this.$message.error("删除失败");
          }
        });
    },

    //导出
    DaoChus() {
      this.axios
        .get("http://localhost:5255/api/IDefective/DaoChu")
        .then((dc) => {
          if (dc.data) {
            this.$message.success("导出成功");
          } else {
            this.$message.error("导出失败");
          }
        });
    },

    //导入
    DaoRun() {
      this.axios
        .get("http://localhost:5255/api/IDefective/UploadUserInfo")
        .then((dc) => {
          if (dc.data) {
            this.$message.success("导出成功");
          } else {
            this.$message.error("导出失败");
          }
        });
    },

    //显示
    Show() {
      this.axios
        .get("http://localhost:5255/api/IDefective/responseDto", {
          params: {
            BianHoa: this.BianHoa,
            DefName: this.DefName,
          },
        })
        .then((dcs) => {
          this.tableData = dcs.data.Data;
        });
    },

    //反填
    FanTian(id) {
      this.axios
        .get("http://localhost:5255/api/IDefective/DefectiveFanTian?id=" + id)
        .then((dc) => {
          this.form.Defective_BianHao = dc.data.Defective_BianHao;
          this.form.Defective_Name = dc.data.Defective_Name;
          this.form.Defective_Img = dc.data.Defective_Img;
          this.form.Defective_Id = id;
        });
    },

    //修改
    Upt() {
      this.axios
        .put("http://localhost:5255/api/IDefective/UpdDefective", this.form)
        .then((cxd) => {
          if (cxd.data) {
            this.$message.success("修改成功");
            this.dialogVisibleUpt = false;
            this.Show();
          } else {
            this.$message.error("修改失败");
          }
        });
    },
  },
  mounted() {
    this.Show();
    // this.FanTian();
  },
};
</script>
<style>
</style>